﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using ENail.Domain.Core;
using ENail.Domain.Core.Contract.Services;
using Equation.Enterprise.Framework.Domain.Data;
using Equation.Enterprise.Utilities.DesignByContract;

namespace ENail.ApplicationServices
{
    public class VendorService : IVendorService
    {
        private readonly IRepository<Vendor> _vendorRepository;

        public VendorService(IRepository<Vendor> vendorRepository)
        {
            Check.Require(vendorRepository != null);

            this._vendorRepository = vendorRepository;
        }

        /// <summary>
        /// Helps view all vendors on list view
        /// </summary>
        /// <returns>List of all vendors</returns>
        public IList<Vendor> GetAllVendors()
        {
            var vendors = _vendorRepository.List();

            if (vendors == null)
                vendors = new List<Vendor>();

            Check.Ensure(vendors != null);

            return vendors.ToList();
        }

        /// <summary>
        /// Helps view vendors for search view
        /// </summary>
        /// <param name="where">Lambda condition to get vendors</param>
        /// <returns></returns>
        public IList<Vendor> GetVendors(Expression<Func<Vendor, bool>> @where)
        {
            var vendors = _vendorRepository.List(@where);

            if (vendors == null)
                vendors = new List<Vendor>();

            Check.Ensure(vendors != null);

            return vendors.ToList();
        }

        /// <summary>
        /// Helps view active vendors only
        /// </summary>
        /// <returns>All active vendors</returns>
        public IList<Vendor> GetAllActiveVendors()
        {
            var vendors = _vendorRepository.List(x => x.IsActive);

            if (vendors == null)
                vendors = new List<Vendor>();

            Check.Ensure(vendors != null);

            return vendors.ToList();
        }

        /// <summary>
        /// Helps view details of an vendor
        /// </summary>
        /// <param name="id">Identity of vendor</param>
        /// <returns>Vendor with specified identity</returns>
        public Vendor GetVendor(int id)
        {
            Check.Require(id > 0);

            Vendor vendor = _vendorRepository.Get(id);

            return vendor;
        }

        /// <summary>
        /// Insert an vendor from view
        /// </summary>
        /// <param name="vendor">Vendor to be inserted</param>
        /// <returns>Number of records affected</returns>
        public int InsertVendor(Vendor vendor)
        {
            Check.Require(vendor != null);

            //_vendorRepository = new DbRepository<Vendor>();
            _vendorRepository.Add(vendor);
            return _vendorRepository.SaveChanges();
        }

        /// <summary>
        /// Update an vendor from view
        /// </summary>
        /// <param name="vendor">Vendor to be updated</param>
        /// <returns>Number of records affected</returns>
        public int UpdateVendor(Vendor vendor)
        {
            Check.Require(vendor != null);

            _vendorRepository.Update(vendor);
            return _vendorRepository.SaveChanges();
        }

        /// <summary>
        /// Helps to inactive an vendor
        /// </summary>
        /// <param name="vendor">Vendor to be inactive</param>
        /// <returns>Number of records affected</returns>
        public int InActiveVendor(Vendor vendor)
        {
            Check.Require(vendor != null);

            vendor.IsActive = false;
            _vendorRepository.Update(vendor);
            return _vendorRepository.SaveChanges();
        }
    }
}
